<?php
/**
 * 文件管理
 */
class FileApp extends CommonApp{

	private $base_dir = null;
	private $filename;//文件名(get传递过来的)
	private $file;//文件路径(物理路径)

	public function __construct(){
		$this->base_dir = dirname(ROOT_DIR);
		$this->filename = urldecode( iconv('utf-8','gbk',My::decode(I('request.file'))) );
		$this->file = ($this->base_dir).$this->filename;//当前操作的文件/文件夹路径
	}

	public function indexAct(){
		$active_path = I('request.active_path');
		if(empty($active_path)){
			$active_path = '/'.basename(ROOT_DIR);
		}
		$active_path = str_replace('\\', '/', $active_path);
		$this->assign('up_path', urlencode(dirname($active_path)));
		$this->assign('active_path', String::auto_charset($active_path));
		$this->assign('list', $this->getDirFile($active_path));

		$this->display();
	}
	 
	 
	/**
	 * 返回指定目录下的文件列表
	 */
	public function getDirFile($path){
		if($path == '/') $path = '';
		$absloute_path = $this->base_dir.$path.'/';
		//echo $absloute_path;
		$dir = opendir($absloute_path);
		$dirs = array();
		$files = array();
		while($file = readdir($dir)){
			if(in_array($file, array('.', '..'))) continue;
			$file_path = $absloute_path.$file;
			$action_path = urlencode($path.'/'.$file);
			$file = iconv('gbk', 'utf-8', $file);
			if(is_dir($file_path)){
				$dirs[] = array('name'=>$file, 'img'=>'dir', 'edit'=>0, 'active_path'=>$action_path);
			}else{
				$size = File::getFileSize(filesize($file_path));
				$date = date('Y-m-d H:i:s',filemtime($file_path));
				$row = array('name'=>$file, 'size'=>$size, 'date'=>$date, 'active_path'=>$action_path);
				$files[] = array_merge($row, $this->matchFile($file));
			}
		}
		closedir($dir);
		return array_merge($dirs, $files);
	}
	 
	/**
	 * 匹配文件格式
	 * @param string $file
	 * @return array
	 */
	private function matchFile($file){
		$reg_arr = array(
			'gif|png' => array('img'=>'gif', 'edit'=>0), 
			'jpg' => array('img'=>'jpg', 'edit'=>0),
			'swf|fla|fly' => array('img'=>'flash', 'edit'=>0),
			'zip|rar|tar.gz' => array('img'=>'zip', 'edit'=>0),
			'exe' => array('img'=>'exe', 'edit'=>0),
			'mp3|wma' => array('img'=>'mp3', 'edit'=>0),
			'wmv|api' => array('img'=>'wmv', 'edit'=>0),
			'rm|rmvb' => array('img'=>'rm', 'edit'=>0),
			'txt|inc|pl|cgi|asp|xml|xsl|aspx|cfm|' => array('img'=>'txt', 'edit'=>1),//最后一个留空,表示没后缀也可以编辑
			'htm|html|tpl' => array('img'=>'htm', 'edit'=>1),
			'php' => array('img'=>'php', 'edit'=>1),
			'js' => array('img'=>'js', 'edit'=>1),
			'css' => array('img'=>'css',  'edit'=>1)
		);
		foreach($reg_arr as $k=>$v){
			if(preg_match("#({$k})#i", $file)){//if(preg_match("#\.({$k})#i", $file)) 去掉了\.因为没有点的文件没有编辑按钮
				return $v;
				break;
			}
		}
		return array('img'=>'file_unknow', 'edit'=>0);
	}
	 
	/**
	 * 编辑文件
	 */
	public function editAct(){
		$file = $this->file;
		if(!file_exists($file) AND is_dir($file))
			$this->ajaxReturn(404, '无法编辑文件夹！');
		
		if($content = I('request.content')){
			if(!is_writable($file)) $this->ajaxReturn(300, '文件无法写入！');
			file_put_contents($file, $content);
			$this->ajaxReturn(200, '文件编辑成功！');
		}else{
			if(!file_exists($file)) $this->ajaxReturn(404, '文件不存在！');
			if(!is_readable($file)) $this->ajaxReturn(404, '文件无法读取！');
			$content = file_get_contents($file);
			if(!@String::is_utf8($content)) $content = iconv('','utf-8',$content);
			$this->assign('filename',iconv('','utf-8',$this->filename)); 
			$this->assign('content', $content);
			$this->display();
		}
	}
	 
	/**
	 * 删除文件
	 */
	public function deleteAct(){
		$file = $this->file;
		if(is_file($file)){
			if(@unlink($file)) $this->ajaxReturn(200, '文件已删除成功！');
		}elseif(is_dir($file)){
			rm_dir($file);
			$this->ajaxReturn(200, '目录已删除成功！');
		}
		$this->ajaxReturn(300, '删除失败，您可能没有权限操作！');
	}
	 
	/**
	 * 创建文件或文件夹
	 */
	public function createAct(){
		$type = My::arr2var($_REQUEST,'type');
		$file = $this->base_dir.$this->request('file');
		$file_name = I('request.file_name');
		if($_POST){
			$path = $file.'/'.$file_name;
			$path = iconv('utf-8','gbk',$path);
			if($type == 'file'){
				if(empty($file_name)){
					$this->ajaxReturn(300, '文件名不能为空！');
				}
				if(file_exists($path)){
				   $this->ajaxReturn(300, '文件已经存在！');
				}
				if(file_put_contents($path, I('post.content',' '))){
					$this->ajaxReturn(200, '文件创建成功！');
				}else{
					$this->ajaxReturn(300, '文件创建失败！');
				}
			}else{
				if(is_dir($path)){
					$this->ajaxReturn(300, '目录已经存在！');
				}
				if(mk_dir($path)){
					$this->ajaxReturn(200, '目录创建成功！');
				}else{
					$this->ajaxReturn(300, '目录创建失败！');
				}
			}
		}
		$this->assign('file', $file);
		$this->display();
	}
	 
	/**
	 * 改名
	 */
	public function renameAct(){
		if($new_name = I('request.new_name')){
			$file = $this->file;
			$new_name = dirname($file).'/'.$new_name;
			if(is_writable($file) && $new_name!=$file){
				if(rename($file, $new_name)){
					$this->ajaxReturn(200, '文件重命名成功！');
				}
			}
			$this->ajaxReturn(200, '文件重命名失败！');
		}else{
			$old_name = iconv('gbk','utf-8',urldecode(My::decode(I('request.file'))));
			$this->assign('old_name', $old_name);
			$this->display();
		}
	}
	 
	/**
	 * 移动
	 */
	public function moveAct(){
		$file = $this->file;
		if($news_dir = I('request.new_dir')){
			$new_dir = rtrim(ltrim($news_dir, '/'), '/');
			if($new_dir == '' || preg_match('#\.\.#', $new_dir)){
				$this->ajaxReturn(300, '路径不合法，请再重新输入！');
			}
			$new_dir = I('request.cur_dir').'/'.$new_dir.'/';
			if(!is_dir($new_dir)){
				mk_dir($new_dir);
			}
			if(is_readable($file) && is_readable($new_dir) && is_writable($new_dir)){
				if(copy($file, $new_dir.basename($file))){
					unlink($file);
					$this->ajaxReturn(200, '文件移动成功！');
				}
			}
			$this->ajaxReturn(300, '文件移动失败！');
		}
		$this->assign('move_name', basename($file));
		$this->assign('cur_dir', dirname($file));
		$this->display();
	}
	
	/**
	 * 空间大小
	 */
	public function spaceAct(){
		$file = $this->file;
		set_time_limit(0);
		$size = File::getFileSize(File::checkFileSize($file));
		echo "<br/><i>目录 <b>{$file}</b> 的使用状况：{$size}</i>";
	}
	 
	/**
	 * 上传文件
	 */
	public function uploadAct(){
		$path = $this->base_dir.I('request.file');
		if($_POST && isset($_FILES) && count($_FILES)>0){
			$upload_boj = new Upload();
			$upload_boj->saveRule = '';
			$upload_boj->allowTypes = '';
			$upload_boj->upload($path.'/');
		}else{
			$this->assign('PHPSESSID', session_id());
			$this->assign('cur_dir', I('request.file'));
			$this->display();
		}
	}

}